package com.example.controller;

import com.example.DTO.AfterSalesApplicationDTO;
import com.example.result.Result;
import com.example.entity.AfterSalesApplication;
import com.example.entity.Buyer;
import com.example.entity.Product;
import com.example.service.AfterSalesApplicationService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/aftersale")
@Tag(name="售后相关接口")
public class AfterSalesApplicationController {
    @Resource
    private AfterSalesApplicationService afterSalesApplicationService;

    /**
     * 买家提交该商品的售后申请，包含订单编号、申请标题、申请描述、申请图片、售后类型（仅退款、退货退款、驳回）、售后状态（已提交）
     * 买家提交后自动为售后增加店铺ID
     * @param application
     * @return
     */
    @PostMapping("/apply")
    @Operation(summary = "提交售后申请接口",description="提交售后申请接口，返回申请ID")
    public Result submitApplication(@RequestBody AfterSalesApplication application) {
        Integer applicationId = afterSalesApplicationService.submitAfterSalesApplication(application);
        if (applicationId!= null) {
            return Result.success("售后申请提交成功，申请ID为：" + applicationId);
        }
        return Result.error("500","售后申请提交失败");
    }


    /**
     * 买家查询该商品的售后状态，售后类型（仅退款、退货退款、驳回） 售后状态（已提交，已处理） 处理结果（仅退款、退货退款、驳回）
     * @param applyId
     * @return
     */
    @GetMapping("/status/{applyId}")
    @Operation(summary = "查询售后申请状态接口",description="查询售后申请状态接口，返回申请详细信息")
    public Result getApplicationStatus(@PathVariable("applyId") Integer applyId) {
        AfterSalesApplication application = afterSalesApplicationService.getApplicationStatus(applyId);
        if (application!= null) {
            return Result.success(application);
        }
        return Result.error("500","未找到对应的售后申请");
    }
    /**
     * 买家获取该订单的所有售后状态
     * @param orderItemsId
     * @return
     */
    @GetMapping("/getAllStatus/{orderItemsId}")
    @Operation(summary = "买家获取该订单的所有售后状态",description="买家获取该订单的所有售后状态")
    public Result<List<AfterSalesApplicationDTO>> getApplicationAllStatus(@PathVariable("orderItemsId") Integer orderItemsId) {
        List<AfterSalesApplicationDTO> list=new ArrayList<>();
        list=afterSalesApplicationService.getApplicationAllStatus(orderItemsId);
        return Result.success(list);
    }

    /**
     * 商家获取自己需要待处理售后列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/pending/{sId}")
    @Operation(summary = "查看待处理售后列表接口",description="查看所有待处理售后列表")
    public Result getAllApplications(@PathVariable Integer sId,
                                     @RequestParam (defaultValue = "1") Integer pageNum ,
                                     @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<AfterSalesApplication> pageInfo= afterSalesApplicationService.getPendingAfterSalesApplications(sId,pageNum,pageSize) ;
        return Result.success(pageInfo);
    }

    /**
     * 商家获取售后申请详情,获取到订单售后的购买者，商品名，以及售后相关信息
     */
    @GetMapping("/detail/{applyId}")
    @Operation(summary = "获取订单售后申请详情",description = "获取订单售后申请详情")
    public Result getApplyIdDetail(@PathVariable Integer applyId){
        AfterSalesApplicationDTO afterSalesApplicationDTO=afterSalesApplicationService.getApplyIdDetail(applyId);
        return Result.success(afterSalesApplicationDTO);
    }


    /**
     * 商家处理售后申请
     */
    @PutMapping("process/{sId}")
    @Operation(summary = "商家处理售后申请接口",description="商家回复售后申请接口，返回申请详细信息")
    public Result replyToApplication(@RequestBody AfterSalesApplication application) {
       afterSalesApplicationService.replyToAfterSalesApplication(application);
        return Result.success();
    }

    /**
     * 商家获取自己的售后历史列表
     */
    @GetMapping("/history/{sId}")
    @Operation(summary = "查看售后历史列表接口",description="查看售后列表接口")
    public Result getHistoryApplications(@PathVariable Integer sId,
            @RequestParam (defaultValue = "1") Integer pageNum ,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<AfterSalesApplication> pageInfo= afterSalesApplicationService.getHistoryAfterSalesApplications(sId,pageNum,pageSize) ;
        return Result.success(pageInfo);
    }
}
